package com.bawei.module_apps.local

import android.util.Log
import androidx.lifecycle.viewModelScope
import com.bawei.library_mvi.model.BaseViewModel
import com.bawei.module_apps.net.NetAppState
import com.bawei.module_apps.net.model.NetAppService
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch

/**
 * @Author : 
 * @Time : On 2023/7/24 10:28
 * @Description : LocalAppsViewModel
 */
class LocalAppsViewModel:BaseViewModel<LocalAppsIntent>() {

    private val repo: LocalAppsRepo by lazy{ LocalAppsRepo() }
    private val _states = MutableStateFlow<LocalAppState>(LocalAppState.Init)//MutableStateFlow可变
    val states: StateFlow<LocalAppState>
        get() = _states
    //处理意图
    override fun handleIntent() {
        viewModelScope.launch {
           intents.consumeAsFlow().collect{
               when(it){
                   is LocalAppsIntent.LocalAppsDataIntent -> getLocalData() //获得本机app
               }
           }
        }
    }

    private fun getLocalData() {
        viewModelScope.launch {
            repo.selInstallApps().collect{
                Log.d("ytx", "getLocalData: "+it.size)
                _states.value = LocalAppState.LocalAppSuccess(it)
            }
        }
    }
}